package com.spring.mvc.controller;

import com.spring.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0.0
 * @Date: 2022/6/20 19:26
 * @Author ZhuYouBin
 * @Description
 */
@Controller
public class AnnotationController {

    // 请求路径为: / 、/index都可以匹配
    // 请求方式为: GET、POST都可以匹配
    @RequestMapping(value = {"/", "/index"}, method = { RequestMethod.GET, RequestMethod.POST })
    public String index() {
        return "index";
    }

    // 只能够匹配GET请求
    @GetMapping(value = {"/", "/get"})
    public String get() {
        return "index";
    }

    // 只能够匹配POST请求
    @PostMapping(value = {"/", "/post"})
    public String post() {
        return "index";
    }

    @PostMapping(value = "/requestBody")
    public String requestBody(@RequestBody User user) {
        // @RequestBody作用: 将请求体中的请求参数和User对象进行映射绑定
        System.out.println("user=" + user);
        return "index";
    }

    @GetMapping(value = "/responseBody")
    @ResponseBody
    public String responseBody() {
        return "@ResponseBody";
    }

    @GetMapping(value = "/pathVariable/{name}")
    public String pathVariable(@PathVariable String name) {
        return "index";
    }

    // 前端请求参数叫: uname，而我们方法参数名称叫: username
    // 通过@RequestParam注解进行参数绑定
    @GetMapping(value = "/requestParam")
    public String requestParam(@RequestParam("uname") String username) {
        return "index";
    }

}
